﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;

using Microsoft.Win32;

using SBPweb.Modules.Modularity;
using SBPweb.Modules.Smartparts;

using SBPweb.MediaCenter.Common.Services;

namespace SBPweb.MediaCenter.Modules.Organizer.WPF.Controls
{
    /// <summary>
    /// Interaction logic for FolderView.xaml
    /// </summary>
    public partial class FolderView : UserControl, ISmartpart
    {

        #region Private fields

        private bool isModal;
        private Aligning align;
        private IModule parentModule;

        #endregion

        #region Protected fields

        FolderFilesView filesView;
        FolderTreeView treeView;

        #endregion

        #region Constructor logic

        public FolderView(IModule parentModule)
        {
            this.parentModule = parentModule;
            InitializeComponent();
            InitializeFolders();
        }

        #endregion
        
        #region Property accessors

        public Aligning Align
        {
            get
            {
                return align;
            }
            set
            {
                align = value;
            }
        }

        public IModule ParentModule
        {
            get
            {
                return parentModule;
            }
        }

        public new bool IsVisible
        {
            get
            {
                return base.IsVisible;
            }
            set
            {
                if (value)
                {
                    base.Visibility = Visibility.Visible;
                }
                else
                {
                    base.Visibility = Visibility.Hidden;
                }
            }
        }

        public bool IsModal
        {
            get
            {
                return isModal;
            }
        }

        public IUriListOpenerService ListOpener
        {
            get
            {
                return this.ParentModule.Services.Get<IUriListOpenerService>();
            }
        }
        #endregion

        #region Events

        public event EventHandler VisibleChanged;

        public event EventHandler EnabledChanged;

        #endregion

        #region Event handlers

        private void InitializeFolders()
        {
            DirectoryInfo dirInfo = new DirectoryInfo("C:\\Users\\Public\\Videos");//\\HD Samples");
            treeView = new FolderTreeView(parentModule, dirInfo);
            treeView.SetValue(Grid.ColumnProperty, 0);
            filesView = new FolderFilesView(parentModule, dirInfo);
            filesView.SetValue(Grid.ColumnProperty, 2);
            grdFolder.Children.Add(treeView);
            grdFolder.Children.Add(filesView);
            InitializeHandlers();
        }

        private void InitializeHandlers()
        {
            treeView.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(FolderTreeView_SelectedItemChanged);
        }

        private void FolderTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            filesView.Folder = treeView.SelectedItem as DirectoryInfo;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string defUri = @"http://www.hotdog.hu/_data/members1/089/539089/video/v%25E1za%2Bhelyett.wmv";
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.FileName = defUri;
            ofd.ValidateNames = false;
            bool? result = ofd.ShowDialog();
            if (result != null && result.Value)
            {
                ParentModule.Services.Get<IOrganizerService>().Open(new Uri(ofd.FileName, UriKind.RelativeOrAbsolute));
            }
        }

        #endregion

    }

}